<?php

namespace App\Containers\AppSection\Moderation\UI\API\Requests;

use App\Containers\AppSection\Moderation\Tasks\FindModerationByIdTask;
use App\Ship\Parents\Requests\Request;
use App\Ship\Rules\ProhibitedRule;
use Illuminate\Validation\Rule;

class UpdateModerationRequest extends Request
{

    /**
     * Define which Roles and/or Permissions has access to this request.
     */
    protected array $access = [
        'permissions' => '',
        'roles' => 'moderator|admin',
    ];

    /**
     * Id's that needs decoding before applying the validation rules.
     */
    protected array $decode = [
        // 'id',
    ];

    /**
     * Defining the URL parameters (e.g, `/user/{id}`) allows applying
     * validation rules on them and allows accessing them like request data.
     */
    protected array $urlParameters = [
        // 'id',
    ];

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        $moderation = app(FindModerationByIdTask::class)->run($this->id);

        $statuses = [];
        if ($moderation->project->hasModerationStatus(
            'projects_returned',
            'projects_collecting',
            'projects_signing_stage'
        )) {
            $statuses = ['projects_checking', 'projects_rejected'];
        } else if ($moderation->project->hasModerationStatus('projects_rejected')) {
            $statuses = ['projects_checking'];
        }

        return [
            'type' => ['nullable', Rule::in($statuses)],

            'remarks' => ['nullable', 'array'],
            'remarks.*.message' => ['required', 'string'],
        ];
    }

    public function active(): bool
    {
        $moderation = app(FindModerationByIdTask::class)->run($this->id);

        return $moderation->project->moderation->id === $moderation->id;
    }

    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return $this->check([
            'hasAccess',
            'active',
        ]);
    }
}
